package com.bloveoacom.weichi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bloveoacom.base.BaseActivity;
import com.bloveoacom.base.UserInfoBean;
import com.bloveoacom.http.HttpMethods;
import com.bloveoacom.http.MessageModel;
import com.bloveoacom.http.RxBus;
import com.bloveoacom.http.RxHelper;
import com.bloveoacom.observer.BaseSubscriber2;
import com.bloveoacom.util.Constant;
import com.bloveoacom.util.GlideApp;
import com.bloveoacom.util.GlideUtil;
import com.bloveoacom.util.StringUtils;
import com.bloveoacom.util.ToastUtil;
import com.bloveoacom.weichi.R;
import com.bloveoacom.weichi.bean.RewardBean;
import com.bloveoacom.weight.DelEditText;
import com.bloveoacom.weight.DrawableCenterTextView;
import com.bloveoacom.weight.LlkjToolBar;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

import static com.bloveoacom.util.DataUtil.DZ;
import static com.bloveoacom.util.DataUtil.HT;
import static com.bloveoacom.util.DataUtil.getReward;
import static com.bloveoacom.util.DataUtil.showMsg;

/**
 * author: yjl
 * e-mail: 1271901673@qq.com
 * time  : 2018/9/21
 * desc  :
 */
public class UserinfoActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    LlkjToolBar mToolbar;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.et_nickname)
    DelEditText etNickname;
    @BindView(R.id.btn_save)
    DrawableCenterTextView btnSave;

    @Override
    public int getLayoutResId() {
        return R.layout.activity_userinfo;
    }

    @Override
    protected View getViewSetTB() {
        return mToolbar;
    }

    @Override
    public void initView() {
        mToolbar.setToolBar("个人信息",
                true, "", R.drawable.ic_back, false, "", LlkjToolBar.isNull);
        etNickname.setText(UserInfoBean.getInstance().getNickName());
        GlideUtil.loadCircleImage(ivHead, StringUtils.getFullUrl(UserInfoBean.getInstance().getHeader(),0));
    }

    @Override
    public void initData() {

    }

    @Override
    public void getBundleExtras(Bundle extras) {

    }

    @Override
    public void initListener() {

    }
    String nickename;
    @OnClick({R.id.iv_head, R.id.btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_head:
                PictureSelector.create(this)
                        .openGallery(PictureMimeType.ofImage())
                        .isCamera(true)
                        .theme(R.style.picture_default_style)
                        .selectionMode(PictureConfig.SINGLE)
                        .previewImage(true)
                        .previewEggs(true)
                        .synOrAsy(false)
                        .enableCrop(true)
                        .compress(true)
                        .minimumCompressSize(100)
                        .glideOverride(160, 160)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
                break;
            case R.id.btn_save:
                nickename = String.valueOf(etNickname.getText()).trim();
                if(StringUtils.isEmpty(nickename)){
                    ToastUtil.makeShortText(mContext,"请输入昵称");
                    return;
                }
                BaseSubscriber2<Object> s2 = HttpMethods.getInstance().getHttpService().updateUser(UserInfoBean.getInstance().getToken(), nickename).compose(RxHelper.getData()).subscribeWith(new BaseSubscriber2<Object>(this) {
                    @Override
                    public void onNext(Object o) {
                        ToastUtil.makeShortText(mContext, "修改成功");
                        UserInfoBean.getInstance().setNickName(nickename);
                        RxBus.getInstance().send(new MessageModel(MessageModel.TU_TYPE_UPDATEUSERINFO));
                    }
                });
                addDispose(s2);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    List<LocalMedia> localMedias = PictureSelector.obtainMultipleResult(data);
                    if (localMedias != null && localMedias.size() > 0) {
                        LocalMedia localMedia = localMedias.get(0);
                        String path = !StringUtils.isEmpty(localMedia.getCompressPath()) ? localMedia.getCompressPath() : localMedia.getPath();
                        LinkedHashMap<String, RequestBody> params = new LinkedHashMap<>();
                         File header =  new File(path);
                        params.put("file\"; filename=\"" + header.getName(), RequestBody.create(MediaType.parse(Constant.MULTIPART_FORM_DATA), header));
                        params.put("token", RequestBody.create(MediaType.parse(Constant.TEXT_FROMDATA), UserInfoBean.getInstance().getToken()));
                        BaseSubscriber2<RewardBean> baseSubscriber2 = HttpMethods.getInstance().getHttpService().uploadImgForHeader(params).compose(RxHelper.getData()).subscribeWith(new BaseSubscriber2<RewardBean>(this) {
                            @Override
                            public void onNext(RewardBean o) {
                                RewardBean rewardBean =  o;
                                UserInfoBean.getInstance().setHeader(rewardBean.getHeaderImg());
                                RxBus.getInstance().send(new MessageModel(MessageModel.TU_TYPE_UPDATEUSERINFO));
                                showMsg(mContext,o,-1,"头像修改成功",HT);
                            }
                        });
                        addDispose(baseSubscriber2);
                        GlideApp.with(this)
                                .load(path)
                                .circleCrop()
                                .into(ivHead);

                    }
                    break;
                default:
                    break;
            }
        }
    }
}
